package com.erp.framework.types;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;

import oracle.sql.BLOB;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.logger.Logger;


public class CustomBlob implements Blob,SimpleConstants{
	private byte[] content;

	public CustomBlob(Blob blob) {
		super();

	}



	public CustomBlob(byte[] content) {
		super();
		// TODO Auto-generated constructor stub
		this.content = content;
	}



	public InputStream getBinaryStream() throws SQLException {
		// TODO Auto-generated method stub

		return new ByteArrayInputStream(content) ;
	}

	public byte[] getBytes(long pos, int length) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public long length() throws SQLException {
		// TODO Auto-generated method stub
		return content.length;
	}

	public static BLOB getBLOB(Connection conn,byte[] content) throws Exception{

		oracle.sql.BLOB ob=null;
		try{
			ob=BLOB.createTemporary(conn,true,oracle.sql.BLOB.DURATION_SESSION);
			ob.putBytes(1,content);
		}catch(Exception e){
			Logger.logExceptions(e);
			ob.freeTemporary();
			 return null;
		}


		return ob;
	}

	public static BLOB getEmptyBLOB(){

		BLOB ob=null;
		try{
			ob=BLOB.empty_lob();

		}catch(Exception e){
			Logger.logExceptions(e);

			 return null;
		}


		return ob;
	}

	public long position(Blob pattern, long start) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public long position(byte[] pattern, long start) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public OutputStream setBinaryStream(long pos) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}



	public int setBytes(long pos, byte[] bytes) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public void truncate(long len) throws SQLException {
		// TODO Auto-generated method stub

	}

	public OutputStream getByteArrayOutputStream(){
		java.io.ByteArrayOutputStream os=null;
		try{
			os = new java.io.ByteArrayOutputStream();
			java.io.InputStream is=new ByteArrayInputStream(content);
			int bytesRead = 0;
			while((bytesRead = is.read()) != -1) {
				os.write(bytesRead);
			}
			is.close();
			os.close();
		}catch(Exception e){
			Logger.logExceptions(e);

			 return null;
		}

		return os;

	}

	public static byte[] customize(Blob blob){
		byte[] data=null;
		BLOB listBlob=null;
		try{
			if(blob!=null){
				listBlob=(BLOB)blob;
				java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();
				java.io.InputStream is = listBlob.getBinaryStream();
				int bytesRead = 0;
				while((bytesRead = is.read()) != -1) {
					os.write(bytesRead);
				}
				data = os.toByteArray();
				is.close();
				os.close();

				if(listBlob.length() == 0){
					data = null;
				}
			}

		}catch(Exception e){
			Logger.logExceptions(e);

			 return null;
		}

		 return data;

	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

}
